package com.wgj.android.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzw.zwcalendar.ZWCalendarView;
import com.jiaai.xui.adapter.XRecyclerViewAdapter;
import com.jiaai.xui.adapter.XViewHolder;
import com.jiaai.xui.adapter.decoration.DividerDecoration;
import com.jiaai.xui.utils.XDateUtils;
import com.jiaai.xui.utils.XDensityUtils;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.dialog.OpenQDHongBaoDialog;
import com.wgj.android.entity.HBDetial;
import com.wgj.android.entity.QDHS;
import com.wgj.android.entity.SignHb;
import com.wgj.android.entity.SignHbBean;
import com.wgj.android.http.WGJAPI;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/1/31.
 */

public class QDActivity extends AppCompatActivity implements ZWCalendarView.SelectListener {
    @BindView(R.id.calendar_previous)
    ImageView calendarPrevious;
    @BindView(R.id.tv_calendar_show)
    TextView tvCalendarShow;
    @BindView(R.id.tv_calendar_today)
    TextView tvCalendarToday;
    @BindView(R.id.calendarView)
    ZWCalendarView calendarView;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private List<QDHS> qdhsArrayList = new ArrayList<>();
    private XRecyclerViewAdapter<QDHS> deliverAdapter;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private HashMap<String, Boolean> sign = new HashMap<>();

    private Boolean isQD=true;
    private String signTime="";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //XStatusBar.setColorNoTranslucent(this, Color.parseColor("#ff4848"));
        super.setContentView(R.layout.activity_qd);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {

        XHttp.obtain().get(WGJAPI.getHbDetial(), null, new HttpCallBack<HBDetial>() {
            @Override
            public void onSuccess(HBDetial hbDetial) {
                List<QDHS> list=hbDetial.getMySignRecord();
                if(list.size()!=0){
                    qdhsArrayList.clear();
                    qdhsArrayList.addAll(list);
                    deliverAdapter.notifyDataSetChanged();
                }
                for(int i=0;i<list.size();i++){
                    signTime=list.get(0).getTime().substring(0,10);
                   String time =list.get(i).getTime();
                    sign.put(time.substring(0,10), true);
                }
                calendarView.setSignRecords(sign);
            }
            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.error("异常");
            }
        });
    }

    private void initView() {
        deliverAdapter = new XRecyclerViewAdapter<QDHS>(recyclerView, qdhsArrayList, R.layout.qd_hs_item) {
            @Override
            protected void bindData(XViewHolder viewHolder, QDHS qdhs, int i) {

                viewHolder.setImageUrl(R.id.avater, qdhs.getAvater());
                viewHolder.setText(R.id.nickName, qdhs.getM_nick());
                viewHolder.setText(R.id.money, qdhs.getTake_money() + "元");
                viewHolder.setText(R.id.tv_time, qdhs.getTime());
            }
        };

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerDecoration(Color.BLACK, XDensityUtils.dp2px(1f)));
        recyclerView.setAdapter(deliverAdapter);

        calendarView.setSelectListener(this);
    }

    @OnClick({R.id.calendar_previous, R.id.tv_calendar_today})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.calendar_previous:
                finish();
                break;
            case R.id.tv_calendar_today:
                isQD=true;
                calendarView.backToday();
                break;
        }
    }

    @Override
    public void change(int year, int month) {
        tvCalendarShow.setText(String.format("%s 年 %s 月", year, month));
    }

    @Override
    public void select(final int year, final int month, final int day, int week) {
        String str=XDateUtils.format(new Date(), "yyyy-MM-dd");
        if(!format(year,month,day).equals(str)){
            return;
        }
        if(!isQD){
            if(!signTime.equals(XDateUtils.format(new Date(), "yyyy-MM-dd"))){
                new OpenQDHongBaoDialog().show(getSupportFragmentManager(), "OpenQDHongBaoDialog");
                initData();
            }else if(signTime.equals(XDateUtils.format(new Date(), "yyyy-MM-dd"))){
                XToast.info("今天已签到",2000);
            }

        }
        isQD=false;
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
    public String format(int year,int month,int day){
        String ret = String.valueOf(year);
        ret += "-";
        ret += month < 10? ("0" + month) : String.valueOf(month);
        ret += "-";
        ret += day < 10 ? "0" + day : String.valueOf(day);
        return ret;
    }

}
